1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 package sun.util.resources;
42
43 import java.io.File;
44 import java.security.AccessController;
45 import java.security.PrivilegedAction;
46 import java.util.Iterator;
47 import java.util.List;
48 import java.util.Locale;
49 import java.util.ResourceBundle;
50 import java.util.StringTokenizer;
51
52 import sun.util.LocaleDataMetaInfo;
53
54
55
56
57
58
59
60
61
62 public class LocaleData {
63
64 private static final String localeDataJarName = "localedata.jar";
65
66
67
68
69 private static class AvailableLocales {
70 static final Locale[] localeList = createLocaleList();
71 }
72
73
74
75
76
77
78
79 public static Locale[] getAvailableLocales() {
80 return AvailableLocales.localeList.clone();
81 }
82
83
84
85
86
87 public static ResourceBundle getCalendarData(Locale locale) {
88 return getBundle("sun.util.resources.CalendarData", locale);
89 }
90
91
92
93
94
95 public static OpenListResourceBundle getCurrencyNames(Locale locale) {
96 return (OpenListResourceBundle)getBundle("sun.util.resources.CurrencyNames", locale);
97 }
98
99
100
101
102
103 public static OpenListResourceBundle getLocaleNames(Locale locale) {
104 return (OpenListResourceBundle)getBundle("sun.util.resources.LocaleNames", locale);
105 }
106
107
108
109
110
111 public static OpenListResourceBundle getTimeZoneNames(Locale locale) {
112 return (OpenListResourceBundle)getBundle("sun.util.resources.TimeZoneNames", locale);
113 }
114
115
116
117
118
119 public static ResourceBundle getCollationData(Locale locale) {
120 return getBundle("sun.text.resources.CollationData", locale);
121 }
122
123
124
125
126
127 public static ResourceBundle getDateFormatData(Locale locale) {
128 return getBundle("sun.text.resources.FormatData", locale);
129 }
130
131
132
133
134
135 public static ResourceBundle getNumberFormatData(Locale locale) {
136 return getBundle("sun.text.resources.FormatData", locale);
137 }
138
139 private static ResourceBundle getBundle(final String baseName, final Locale locale) {
140 return (ResourceBundle) AccessController.doPrivileged(new PrivilegedAction() {
141 public Object run() {
142 return ResourceBundle.
143 getBundle(baseName, locale,
144 LocaleDataResourceBundleControl.getRBControlInstance());
145 }
146 });
147 }
148
149 static class LocaleDataResourceBundleControl extends ResourceBundle.Control {
150
151 private static LocaleDataResourceBundleControl rbControlInstance =
152 new LocaleDataResourceBundleControl();
153
154 public static LocaleDataResourceBundleControl getRBControlInstance() {
155 return rbControlInstance;
156 }
157
158
159
160
161
162
163
164
165
166
167
168 @Override
169 public List<Locale> getCandidateLocales(String baseName, Locale locale) {
170 List<Locale> candidates = super.getCandidateLocales(baseName, locale);
171
172 String localeString = LocaleDataMetaInfo.getSupportedLocaleString(baseName);
173
174 if (localeString.length() == 0) {
175 return candidates;
176 }
177
178 for (Iterator<Locale> l = candidates.iterator(); l.hasNext(); ) {
179 Locale loc = l.next();
180 String lstr = null;
181 if (loc.getScript().length() > 0) {
182 lstr = loc.toLanguageTag().replace('-', '_');
183 } else {
184 lstr = loc.toString();
185 int idx = lstr.indexOf("_#");
186 if (idx >= 0) {
187 lstr = lstr.substring(0, idx);
188 }
189 }
190
191
192
193
194
195 if (lstr.length() != 0 && localeString.indexOf(" " + lstr + " ") == -1) {
196 l.remove();
197 }
198 }
199 return candidates;
200 }
201
202
203
204
205
206
207
208
209
210 @Override
211 public Locale getFallbackLocale(String baseName, Locale locale) {
212 if (baseName == null || locale == null) {
213 throw new NullPointerException();
214 }
215 return null;
216 }
217 }
218
219
220
221
222
223
224 private static boolean isNonEuroLangSupported() {
225 final String sep = File.separator;
226 String localeDataJar =
227 java.security.AccessController.doPrivileged(
228 new sun.security.action.GetPropertyAction("java.home")) +
229 sep + "lib" + sep + "ext" + sep + localeDataJarName;
230
231
232
233
234 final File f = new File(localeDataJar);
235 boolean isNonEuroResJarExist =
236 AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
237 public Boolean run() {
238 return Boolean.valueOf(f.exists());
239 }
240 }).booleanValue();
241
242 return isNonEuroResJarExist;
243 }
244
245
246
247
248
249
250
251 private static Locale[] createLocaleList() {
252 String supportedLocaleString = LocaleDataMetaInfo.
253 getSupportedLocaleString("sun.text.resources.FormatData");
254
255 if (supportedLocaleString.length() == 0) {
256 return null;
257 }
258
259
260 int barIndex = supportedLocaleString.indexOf("|");
261 StringTokenizer localeStringTokenizer = null;
262 if (isNonEuroLangSupported()) {
263 localeStringTokenizer = new
264 StringTokenizer(supportedLocaleString.substring(0, barIndex) +
265 supportedLocaleString.substring(barIndex + 1));
266 } else {
267 localeStringTokenizer = new
268 StringTokenizer(supportedLocaleString.substring(0, barIndex));
269 }
270
271 Locale[] locales = new Locale[localeStringTokenizer.countTokens()];
272 for (int i = 0; i < locales.length; i++) {
273 String currentToken = localeStringTokenizer.nextToken().replace('_','-');
274 if (currentToken.equals("ja-JP-JP")) {
275 currentToken = "ja-JP-u-ca-japanese-x-lvariant-JP";
276 } else if (currentToken.equals("th-TH-TH")) {
277 currentToken = "th-TH-u-nu-thai-x-lvariant-TH";
278 } else if (currentToken.equals("no-NO-NY")) {
279 currentToken = "no-NO-x-lvariant-NY";
280 }
281 locales[i] = Locale.forLanguageTag(currentToken);
282 }
283 return locales;
284 }
285
286 }